package com.xwnet.org.web.controller;

import com.xwnet.basic.util.AjaxResult;
import com.xwnet.basic.util.PageList;
import com.xwnet.org.domain.Employee;
import com.xwnet.org.query.EmployeeQuery;
import com.xwnet.org.service.IEmployeeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/employee")
@Api(value = "员工的API",description="员工相关的CRUD功能")
public class EmployeeController {
    @Autowired
    private IEmployeeService employeeService;

    //添加或者修改
    @PutMapping
    @ApiOperation(value = "添加或者修改员工")
    public AjaxResult addOrUpdate(@RequestBody Employee employee){
        try {
            if(employee.getId() == null){
                employeeService.add(employee);
            }
                employeeService.update(employee);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("保存失败" + e.getMessage());
        }
    }
    //删除
    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            employeeService.delete(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("删除失败" + e.getMessage());
        }
    }
    //批量删除员工
    @PatchMapping
    public AjaxResult batchDelete(@RequestBody List<Long> ids){
        try {
            employeeService.batchDel(ids);
            return AjaxResult.me();
        }catch (Exception e){
            //链式编程
            return AjaxResult.me().setSuccess(false)
                    .setMessage("操作失败"+e.getMessage());
        }
    }
    //查询某个员工信息
    @GetMapping("/{id}")
    public AjaxResult getById(@PathVariable("id") Long id){
        try {
            Employee employee = employeeService.getById(id);
            return new AjaxResult().setResult(employee);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("查询某个员工信息失败" + e.getMessage());
        }
    }
    //查询员工对应的部门信息
    @GetMapping("/path/{id}")
    public AjaxResult getPath(@PathVariable("id") Long pathId){
        try {
            String path = employeeService.getPath(pathId);
            return new AjaxResult().setResult(path);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("查询某个员工信息失败" + e.getMessage());
        }
    }
    //查询所有员工信息
    @GetMapping
    public AjaxResult getAll(){
        try {
            List<Employee> employees = employeeService.getAll();
            return new AjaxResult().setResult(employees);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("查询所有员工信息失败" + e.getMessage());
        }
    }
    //查询所有员工分页信息
    @PostMapping
    public AjaxResult pageQuery(@RequestBody EmployeeQuery query){
        try {
            PageList<Employee> pagequery = employeeService.pagequery(query);
            return new AjaxResult().setResult(pagequery);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("员工信息分页失败" + e.getMessage());
        }
    }


}
